package com.mmd.base.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

public class AmapUtils {

    public static String GET_ADDRESS_KEY = "eba62f56d367964c13749fa73d1fca7f";
    public static String GET_ADDRESS_URL = "https://restapi.amap.com/v3/geocode/regeo";

    //根据经纬度查询地址
    public static RegionCommon getAddressByRegion(String longitude, String latitude) {
        if (StringUtils.isEmpty(longitude))return new RegionCommon();
        RegionCommon region = new RegionCommon();
        String parameters = "key=" + GET_ADDRESS_KEY + "&location=" + longitude + "," + latitude;
        String s = HttpUtils.sendGet(GET_ADDRESS_URL, parameters);
        JSONObject jsonObject = JSONObject.parseObject(s);
        String regeocode = jsonObject.getString("regeocode");
        JSONObject json = JSONObject.parseObject(regeocode);
        String addressComponent = json.getString("addressComponent");
        JSONObject jsonssss = JSONObject.parseObject(addressComponent);
        String province = jsonssss.getString("province");
        String city = jsonssss.getString("city");
        String district = jsonssss.getString("district");
        region.setCity(city.equals("[]")?province:city);
        region.setPro(province);
        region.setRegion(district);
        System.out.println(region.toString()+"------------------------------------------------------获取的地址");
        return region;
    }
}
